package product;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;

import java.awt.Font;
import java.awt.Dimension;

import javax.swing.JPanel;

import java.awt.SystemColor;

import javax.swing.border.LineBorder;

import java.awt.Cursor;

import javax.swing.DebugGraphics;
import javax.swing.ImageIcon;

import java.awt.ComponentOrientation;
import java.awt.Component;

public class DeliveryRecall extends JInternalFrame {
	ProductMain4 main;
	DecimalFormat df = new DecimalFormat("###,###,###");
	int recallEaHap = 0;
	int EaHap = 0;
	int volume = 0;

	private JTextField textEa;
	private JTextField textMemberID;
	private JTextField textCusNum;
	private JTextField textCode;
	JTextField textDeliveryNum;
	private JButton btnSearch;
	private JButton btnMemBer;
	private JLabel lblEa;
	JLabel lblstate;
	private JButton btnAllSearch;
	private JLabel label;
	private JLabel lblProductCode;
	private JLabel label_5;
	private JLabel label_4;
	private JLabel lblEmployeeId;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel lblDeliveryRecallInput;
	private JLabel lblDeliveryRecallInput_1;
	private JButton btnInput;

	/**
	 * Create the frame.
	 */
	public DeliveryRecall(ProductMain4 pm) {
		super("Retrun Merchandise", false, true, false, true);
		getContentPane().setBackground(Color.WHITE);
		this.main = pm;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 560, 496);
		getContentPane().setLayout(null);
		getContentPane().add(getLblstate());
		getContentPane().add(getBtnAllSearch());
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_1());
		getContentPane().add(getLabel_2_1());

	}

	public void textFieldReset() {
		textCode.setText("");
		textCusNum.setText("");
		textDeliveryNum.setText("");
		textEa.setText("");
		textMemberID.setText("");
		lblEa.setText("");
	}

	public void deliverySearch() {
		recallEaHap = 0;
		EaHap = 0;
		volume = 0;

		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn=o.conn;

			String sql = "select ea from delivery_recall where delivery_num = ?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textDeliveryNum.getText());

			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				recallEaHap = recallEaHap
						+ Integer.parseInt(rs.getString("ea"));
			}

			rs.close();
			pstmt.close();
			// main.conn.close();
		} catch (Exception e) {
		}

		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn=o.conn;

			String sql = "select num, customer_num, code, ea from delivery where num=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textDeliveryNum.getText());

			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				textDeliveryNum.setText(rs.getString(1));
				textCusNum.setText(rs.getString(2));
				textCode.setText(rs.getString(3));
				EaHap = rs.getInt(4);
				volume = EaHap - recallEaHap;
				lblEa.setText("남은 납품 수량 :" + df.format(volume));
			} else {
				JOptionPane.showMessageDialog(null, "제품이 없습니다.");
				textFieldReset();
			}

			rs.close();
			pstmt.close();
			// main.conn.close();

		} catch (Exception ee) {
		}
	}

	public void memberSearch() {
		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn=o.conn;

			String sql = "select irum from memberdata where memberid=?";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textMemberID.getText());
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				String name = rs.getString(1);

				lblstate.setText(name + "님 확인되셨습니다.");
				btnInput.setEnabled(true);
			} else {
				lblstate.setText("해당 아이디가 없습니다.");
				btnInput.setEnabled(false);
			}
			rs.close();
			pstmt.close();
			// main.conn.close();

		} catch (Exception ee) {
		}
	}

	public void recallInput() {
		try {
			// OracleJdbc o = new OracleJdbc();
			// main.conn=o.conn;

			String sql = "insert into delivery_recall(num, ea, member_id, delivery_num, customer_num, code, nal) values (dr_num.nextval,?,?,?,?,?,sysdate)";
			PreparedStatement pstmt = main.conn.prepareStatement(sql);
			pstmt.setString(1, textEa.getText());
			pstmt.setString(2, textMemberID.getText());
			pstmt.setString(3, textDeliveryNum.getText());
			pstmt.setString(4, textCusNum.getText());
			pstmt.setString(5, textCode.getText());

			pstmt.executeUpdate();

			lblstate.setText("반품하였습니다.");
			textFieldReset();

			if (main.tList != null) {
				main.tList.searchData();
			}

			pstmt.close();
			// main.conn.close();

		} catch (Exception e) {

		}
	}

	private JTextField getTextEa() {
		if (textEa == null) {
			textEa = new JTextField();
			textEa.setHorizontalAlignment(SwingConstants.RIGHT);
			textEa.setBorder(new LineBorder(Color.GRAY));
			textEa.setBounds(174, 148, 170, 30);
			textEa.setColumns(10);
		}
		return textEa;
	}

	private JTextField getTextMemberID() {
		if (textMemberID == null) {
			textMemberID = new JTextField();
			textMemberID.setBorder(new LineBorder(Color.GRAY));
			textMemberID.setBounds(174, 189, 170, 30);
			textMemberID.setColumns(10);
		}
		return textMemberID;
	}

	private JTextField getTextCusNum() {
		if (textCusNum == null) {
			textCusNum = new JTextField();
			textCusNum.setBorder(new LineBorder(Color.GRAY));
			textCusNum.setBounds(174, 108, 170, 30);
			textCusNum.setColumns(10);
		}
		return textCusNum;
	}

	private JTextField getTextCode() {
		if (textCode == null) {
			textCode = new JTextField();
			textCode.setBorder(new LineBorder(Color.GRAY));
			textCode.setBounds(174, 69, 170, 30);
			textCode.setColumns(10);
		}
		return textCode;
	}

	private JTextField getTextDeliveryNum() {
		if (textDeliveryNum == null) {
			textDeliveryNum = new JTextField();
			textDeliveryNum.setBorder(new LineBorder(Color.GRAY));
			textDeliveryNum.setBounds(174, 28, 170, 30);
			textDeliveryNum.setColumns(10);
		}
		return textDeliveryNum;
	}

	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.setBorder(new LineBorder(Color.GRAY));
			btnSearch.setForeground(Color.DARK_GRAY);
			btnSearch.setFont(new Font("Microsoft JhengHei UI", Font.BOLD, 15));
			btnSearch.setBounds(375, 28, 120, 30);
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					deliverySearch();
				}
			});
		}
		return btnSearch;
	}

	private JButton getBtnMemBer() {
		if (btnMemBer == null) {
			btnMemBer = new JButton("Search");
			btnMemBer.setBorder(new LineBorder(Color.GRAY));
			btnMemBer.setCursor(Cursor
					.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			btnMemBer.setOpaque(false);
			btnMemBer.setForeground(Color.DARK_GRAY);
			btnMemBer.setFont(new Font("Microsoft JhengHei UI", Font.BOLD, 15));
			btnMemBer.setBounds(375, 188, 120, 30);
			btnMemBer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					memberSearch();
				}
			});
		}
		return btnMemBer;
	}

	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("");
			lblEa.setHorizontalAlignment(SwingConstants.CENTER);
			lblEa.setBorder(new LineBorder(Color.GRAY));
			lblEa.setBackground(new Color(255, 192, 203));
			lblEa.setOpaque(true);
			lblEa.setBounds(375, 148, 120, 30);
			lblEa.setForeground(new Color(255, 0, 0));
		}
		return lblEa;
	}

	private JLabel getLblstate() {
		if (lblstate == null) {
			lblstate = new JLabel("");
			lblstate.setBorder(new LineBorder(Color.GRAY, 2));
			lblstate.setForeground(new Color(255, 0, 0));
			lblstate.setBackground(SystemColor.info);
			lblstate.setOpaque(true);
			lblstate.setBounds(12, 423, 519, 30);
		}
		return lblstate;
	}

	private JButton getBtnAllSearch() {
		if (btnAllSearch == null) {
			btnAllSearch = new JButton("Delivery List View");
			btnAllSearch.setBorder(new LineBorder(Color.GRAY));
			btnAllSearch.setForeground(Color.DARK_GRAY);
			btnAllSearch.setFont(new Font("Microsoft JhengHei UI", Font.BOLD,
					15));
			btnAllSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					main.openDeliveryList();
				}
			});
			btnAllSearch.setBounds(368, 65, 163, 30);
		}
		return btnAllSearch;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Delivery Number");
			label.setBounds(24, 28, 170, 30);
			label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
		}
		return label;
	}

	private JLabel getLblProductCode() {
		if (lblProductCode == null) {
			lblProductCode = new JLabel("Product Code");
			lblProductCode.setBounds(24, 68, 170, 30);
			lblProductCode.setFont(new Font("Microsoft YaHei UI", Font.PLAIN,
					15));
		}
		return lblProductCode;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("Quantity");
			label_5.setBounds(24, 148, 170, 30);
			label_5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
		}
		return label_5;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("Registration Number ");
			label_4.setBounds(24, 108, 170, 30);
			label_4.setPreferredSize(new Dimension(70, 20));
			label_4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
		}
		return label_4;
	}

	private JLabel getLblEmployeeId() {
		if (lblEmployeeId == null) {
			lblEmployeeId = new JLabel("Employee ID");
			lblEmployeeId.setBounds(24, 188, 170, 30);
			lblEmployeeId.setHorizontalAlignment(SwingConstants.LEFT);
			lblEmployeeId.setFont(new Font("Microsoft JhengHei UI", Font.PLAIN,
					15));
		}
		return lblEmployeeId;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new LineBorder(Color.GRAY, 2));
			panel.setBackground(SystemColor.inactiveCaptionBorder);
			panel.setBounds(12, 98, 519, 319);
			panel.setLayout(null);
			panel.add(getLabel());
			panel.add(getLblProductCode());
			panel.add(getLabel_4());
			panel.add(getLabel_5());
			panel.add(getLblEmployeeId());
			panel.add(getTextDeliveryNum());
			panel.add(getTextCode());
			panel.add(getTextCusNum());
			panel.add(getTextEa());
			panel.add(getTextMemberID());
			panel.add(getLblEa());
			panel.add(getBtnSearch());
			panel.add(getBtnMemBer());
			panel.add(getBtnInput());

		}
		return panel;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(-21, -21, 702, 74);
			panel_1.add(getLabel_1_1());
		}
		return panel_1;
	}

	private JLabel getLabel_1_1() {
		if (lblDeliveryRecallInput == null) {
			lblDeliveryRecallInput = new JLabel("Retrun Merchandise Input");
			lblDeliveryRecallInput.setFont(new Font("Microsoft YaHei UI",
					Font.PLAIN, 40));
			lblDeliveryRecallInput.setBounds(64, 13, 543, 61);
		}
		return lblDeliveryRecallInput;
	}

	private JLabel getLabel_2_1() {
		if (lblDeliveryRecallInput_1 == null) {
			lblDeliveryRecallInput_1 = new JLabel(
					"Retrun Merchandise Information");
			lblDeliveryRecallInput_1.setForeground(Color.DARK_GRAY);
			lblDeliveryRecallInput_1.setFont(new Font("Microsoft YaHei UI",
					Font.PLAIN, 20));
			lblDeliveryRecallInput_1.setBounds(12, 63, 322, 30);
		}
		return lblDeliveryRecallInput_1;
	}

	private JButton getBtnInput() {
		if (btnInput == null) {
			btnInput = new JButton("Save");
			btnInput.setEnabled(false);
			btnInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (volume > Integer.parseInt(textEa.getText())) {
						recallInput();

					} else {
						JOptionPane.showMessageDialog(null, "재고보다 많이 입력하셨습니다.");
					}
				}
			});
			btnInput.setBounds(176, 254, 168, 55);
			btnInput.setBorder(new LineBorder(Color.GRAY));
			btnInput.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
		}
		return btnInput;
	}
}
